<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN">
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<title>B0-based remote API modus operandi</title>
<link rel="stylesheet" type="text/css" href="../style.css">
</head>

<body>

<div align="center">
<table class=allEncompassingTable >
 <tr>
  <td >
<p><a href="../index.html" TARGET="_top"><img src="images/homeImg.png"></a></p>



<h1>B0-based remote API modus operandi</h1>

<p class=warningBox>The B&Oslash;-based remote API should not be mixed-up with the <a href="legacyRemoteApiOverview.htm">legacy remote API</a> (or simply <em>remote API</em>), which is an older version of the remote API that is less flexible, and more difficult to extend.</p>

<p>A B0-based remote API function is called in a similar fashion as a <a href="apiOverview.htm">regular API</a> function, however with one major difference:</p>
<p>Most B0-based remote API functions require one additional argument: the topic or communication channel to use for executing the function call. The topic can be the return value of one of following 5 functions:</p>
<li><a href="b0RemoteApi-cpp.htm#simxServiceCall">simxServiceCall</a>: this topic allows to execute the function in a blocking mode, i.e. the command will travel to the server (i.e. CoppeliaSim), execute there, and return a response to the client. Use this topic only when fetching command responses from the server as a one-time operation (e.g. <a href="b0RemoteApi-cpp.htm#simxGetObjectHandle">simxGetObjectHandle</a> would typically use a service call to execute).</li>
<li><a href="b0RemoteApi-cpp.htm#simxDefaultPublisher">simxDefaultPublisher</a>: this topic allows to execute the function in a non-blocking mode, i.e. the function is sent to the server (i.e. CoppeliaSim), and control returns immediately to the client (i.e. the client will not wait for a reply from the server). Use this topic only when sending commands to the server, where you do not expect/need a response from it (e.g. <a href="b0RemoteApi-cpp.htm#simxSetJointPosition">simxSetJointPosition</a> would typically use the default publisher to execute).</li>
<li><a href="b0RemoteApi-cpp.htm#simxDefaultSubscriber">simxDefaultSubscriber</a>: this topic informs the server to continuously execute the function, and continuously stream the response to the client. The client will be receiving the response inside of a callback function. Use this topic only when you want to receive the response from the same command continuously executed on the server side. (e.g. <a href="b0RemoteApi-cpp.htm#simxGetJointForce">simxGetJointForce</a> would typically use the default subscriber to execute). Defined callback functions are called via the <a href="b0RemoteApi-cpp.htm#simxSpinOnce">simxSpinOnce</a> function (when a response is available in the input buffer). </li>
<li><a href="b0RemoteApi-cpp.htm#simxCreatePublisher">simxCreatePublisher</a>: this is very similar to <em>simxDefaultPublisher</em>, with the difference, that a dedicated publisher topic is created, i.e. a dedicated publishing channel is created. It can be useful to assign specific functions/commands to a dedicated publisher, specially with heavy data (e.g. <a href="b0RemoteApi-cpp.htm#simxSetVisionSensorImage">simxSetVisionSensorImage</a> would typically use a dedicated publisher to execute).</li>
<li><a href="b0RemoteApi-cpp.htm#simxCreateSubscriber">simxCreateSubscriber</a>:  this is very similar to <em>simxDefaultSubscriber</em>, with the difference, that a dedicated subscriber topic is created, i.e. a dedicated subscriber channel is created. It can be useful to assign specific functions/commands to a dedicated subscriber, specially with heavy data (e.g. <a href="b0RemoteApi-cpp.htm#simxGetVisionSensorImage">simxGetVisionSensorImage</a> would typically use a dedicated subscriber to execute).</li>

<br>



<p><a name="synchronous" id="synchronous"></a>By default, the B0-based remote API client and the server (i.e. CoppeliaSim) will run asynchronously. It is however possible to have the client trigger each simulation step individually, in order to achieve a synchronous operation. Following is a Python example of the synchronous mode:</p>

<pre class=lightPurpleBox>
import b0RemoteApi
import time

with b0RemoteApi.RemoteApiClient('b0RemoteApi_pythonClient','b0RemoteApi') as client:    
    doNextStep=True

    def simulationStepStarted(msg):
        simTime=msg[1][b'simulationTime'];
        print('Simulation step started. Simulation time: ',simTime)
        
    def simulationStepDone(msg):
        simTime=msg[1][b'simulationTime'];
        print('Simulation step done. Simulation time: ',simTime);
        global doNextStep
        doNextStep=True
        
    client.simxSynchronous(True)
    client.simxGetSimulationStepStarted(client.simxDefaultSubscriber(simulationStepStarted));
    client.simxGetSimulationStepDone(client.simxDefaultSubscriber(simulationStepDone));
    client.simxStartSimulation(client.simxDefaultPublisher())
    
    startTime=time.time()
    while time.time()&lt;startTime+5: 
        if doNextStep:
            doNextStep=False
            client.simxSynchronousTrigger()
        client.simxSpinOnce()
    client.simxStopSimulation(client.simxDefaultPublisher())</pre>

<br>
<br>
<h3 class=recommendedTopics>Recommended topics</h3>
<li><a href="b0RemoteApiOverview.htm">B0-based remote API overview</a></li>
<li><a href="b0RemoteApiExtension.htm">Extending the B0-based remote API</a></li>
<br>
<br>
 </tr>
 <tr>
   <td > 
 </tr>
</table> 
</div>  
  
  
</body>

</html>
